//
//  HTSubscribe.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/18.
//

import Foundation
import HandyJSON

struct HTSubscribe: HandyJSON {
  var var_productIdentifier: String?// 产品标识
  var var_subscriptionGroupIdentifier: String?// 订阅群组标识
  var var_title: String?// 标题
  var var_desc: String?// 描述
  var var_origin_price: NSDecimalNumber?// 订阅默认价格
  var var_price: NSDecimalNumber?// 订阅价格
  var var_eachPrice: NSDecimalNumber?// 订阅每日价格
  var var_introductoryPrice: NSDecimalNumber?// 订阅促销价格
  var var_introductory_unit: String?// 订阅促销时长
  var var_isBestPrice: Bool? = false// 是否是最优惠价格
  var var_expiredDate: String?// 过期时间
  var var_auto_renew_status: Int = 1// 1续订 0不续订

  func ht_getPayPrice() -> NSDecimalNumber? {
    if (self.var_introductoryPrice != nil) {
      return self.var_introductoryPrice ?? 0
    }

    return self.var_price ?? 0
  }

  func ht_discountPercentage() -> String? {
    if (self.var_introductoryPrice != nil && self.var_price != nil) {
      let percentage = self.var_price!.subtracting(self.var_introductoryPrice!).dividing(by: self.var_price!).multiplying(by: 100)
      return "\(Int(round(percentage.doubleValue)))%"
    }

    return "0%"
  }

  func ht_eachPrice() -> String {
    if (self.var_eachPrice != nil) {
      let numberFormatter = NumberFormatter()
      numberFormatter.numberStyle = .decimal
      numberFormatter.maximumFractionDigits = 2

      if let formattedNumber = numberFormatter.string(from: self.var_eachPrice!) {
        return formattedNumber
      }
      return "-"
    }

    return "-"
  }

  func ht_selTip() -> String? {
    if (self.var_introductoryPrice != nil && self.var_price != nil) {
      return "* $\(self.var_introductoryPrice ?? 0) for the 1st \(self.var_introductory_unit ?? ""), then $\(self.var_price ?? 0)/\(self.var_desc ?? ""), "
    }

    return ""
  }
}
